package com.weixin.engine.design.single.lazy;

import java.util.concurrent.*;

/**
 * 非线程安全演示
 */
public class LazyUnsafeSingletionTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        for (int i = 0; i < 100; i++) {
//            new Thread(() -> {
//                System.out.println(LazyUnsafeSingleton.getInstance());
//            }).start();
//        }

        countDown();
    }

    private static void countDown() {
        //启动一百个线程同时去抢占cpu，有可能产生并发
        int count = 10000;

        //利用发令枪操作
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                //展示时间戳+引用的单例
                //以下Singleton4为之前博客写的用静态类实现的那种
                System.out.println(System.currentTimeMillis() + ":" + LazyUnsafeSingleton.getInstance());
            }).start();
            //递减锁存器的计数，如果计数到达零，则释放所有等待的线程
            countDownLatch.countDown();
        }
        try {
            //使线程在锁存器倒计数至零之前一直等待
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}